package com.logistics.service.Impl;

import com.logistics.dao.BaseDao;
import com.logistics.dao.CargoDao;
import com.logistics.dao.TransportTaskDao;
import com.logistics.dao.VehicleDao;
import com.logistics.entity.Cargo;
import com.logistics.entity.TransportTask;
import com.logistics.service.TransportTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Set;

/**
 * Created by Ranger on 2015/8/14.
 */
@Repository
public class TransportTaskServiceImpl extends BaseServiceImpl<TransportTask> implements TransportTaskService {
    @Autowired
    private TransportTaskDao transportTaskDao;
    @Autowired
    private CargoDao cargoDao;
    @Autowired
    private VehicleDao vehicleDao;

    @Override
    public BaseDao getBaseDao() {
        return transportTaskDao;
    }

    public TransportTask publishTransTask(Set<Cargo> cargoSet, TransportTask transportTask) {
        // 检测用户输入的运输单数据
        if (transportTask.getStartPlace().isEmpty() || transportTask.getEndPlace().isEmpty()) {
            return null;
        }
        // 检测用户输入的货物数据
        for (Cargo cargo : cargoSet) {
            if (cargo.getName().isEmpty() || cargo.getWeight() <= 0) {
                return null;
            }
        }
        // 保存运输单数据
        transportTaskDao.save(transportTask);
        for (Cargo cargo : cargoSet) {
            cargo.setTask(transportTask);
            cargoDao.save(cargo);
        }
        transportTask.setCargoSet(cargoSet);
        return transportTask;
    }
}